/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

/**
 *
 * @author EnzoZhong
 */
public class Download extends Thread implements Runnable {

     private String imgUrl;
     private String fileURL;

     public Download ( String url , String filePath ) {
          this.imgUrl = url;
          this.fileURL = filePath;
     }

     @Override
     public void run () {
          try {
               BufferedOutputStream out;
               try ( final BufferedInputStream in = new BufferedInputStream ( new URL ( imgUrl ).openStream () ) ) {
                    int index = imgUrl.lastIndexOf ( '/' );
                    String sName = imgUrl.substring ( index + 1 , imgUrl.length () );
                    File img = new File ( fileURL + sName );
                    out = new BufferedOutputStream ( new FileOutputStream ( img ) );
                    byte[] buf = new byte[ 19890310 ];
                    int length = in.read ( buf );
                    while ( length != -1 ) {
                         out.write ( buf , 0 , length );
                         length = in.read ( buf );
                    }
                    System.out.println ( this.getName () + " " + sName );
               }
               out.close ();
          } catch ( Exception e ) {
               System.err.println ( e );
          }
     }

}
